home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TABCNTRL.PAK / TABCTRL.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  11KB  |  452 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   tabctrl.c
  9. //
  10. //  PURPOSE:  Implements the window procedure for the main application
  11. //            window.  
  12. //
  13. //  FUNCTIONS:
  14. //    WndProc      - Processes messages for the main window.
  15. //    ChildWndProc - Processes messages for the child window.
  16. //    MsgCommand   - Handle the WM_COMMAND messages for the main window.
  17. //    MsgDestroy   - Handles the WM_DESTROY message by calling 
  18. //                   PostQuitMessage().
  19. //    MsgNotify    - Handle the WM_NOTIFY message
  20. //    CmdDemo      - Handles the Demo.TabControl menuitem
  21. //    CmdExit      - Handles the Demo.Exit menuitem
  22. //    ChildMsgPaint- Handle the WM_PAINT case for the child window
  23. //
  24. //  COMMENTS:
  25. //
  26.  
  27. #include <windows.h>          // required for all Windows applications
  28. #include <commctrl.h>         // required for applications using Shell controls.
  29. #include "globals.h"          // prototypes specific to this application
  30. #include "resource.h"
  31. #include "demo.h"
  32.  
  33.  
  34. #define  NUMTABS 4
  35.  
  36. extern HWND hwndDisplayChild;
  37.  
  38. LRESULT MsgNotify(HWND, UINT, WPARAM, LPARAM);
  39.  
  40. // Child window message table definition.
  41. MSD rgmsdChild[] =
  42. {
  43.     {WM_PAINT, ChildMsgPaint}
  44. };
  45.  
  46. MSDI msdiChild =
  47. {
  48.     sizeof(rgmsdChild) / sizeof(MSD),
  49.     rgmsdChild,
  50.     edwpWindow
  51. };
  52.  
  53. // Main window message table definition.
  54. MSD rgmsd[] =
  55. {
  56.     {WM_COMMAND, MsgCommand},
  57.     {WM_NOTIFY,  MsgNotify},
  58.     {WM_DESTROY, MsgDestroy}
  59. };
  60.  
  61. MSDI msdiMain =
  62. {
  63.     sizeof(rgmsd) / sizeof(MSD),
  64.     rgmsd,
  65.     edwpWindow
  66. };
  67.  
  68. // Main window command table definition.
  69. CMD rgcmd[] =
  70. {
  71.     {IDM_DEMOTABCTRL, CmdDemo},
  72.     {IDM_EXIT,        CmdExit},
  73.     {IDM_ABOUT,       CmdAbout}
  74. };
  75.  
  76. CMDI cmdiMain =
  77. {
  78.     sizeof(rgcmd) / sizeof(CMD),
  79.     rgcmd,
  80.     edwpWindow
  81. };
  82.  
  83.  
  84. //
  85. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  86. //
  87. //  PURPOSE:  Processes messages for the main window.
  88. //
  89. //  PARAMETERS:
  90. //    hwnd     - window handle
  91. //    uMessage - message number
  92. //    wparam   - additional information (dependant on message number)
  93. //    lparam   - additional information (dependant on message number)
  94. //
  95. //  RETURN VALUE:
  96. //    The return value depends on the message number.  If the message
  97. //    is implemented in the message dispatch table, the return value is
  98. //    the value returned by the message handling function.  Otherwise,
  99. //    the return value is the value returned by the default window procedure.
  100. //
  101. //  COMMENTS:
  102. //    Call the DispMessage() function with the main window's message dispatch
  103. //    information (msdiMain) and the message specific information.
  104. //
  105.  
  106. LRESULT CALLBACK WndProc(HWND   hwnd, 
  107.                          UINT   uMessage, 
  108.                          WPARAM wparam, 
  109.                          LPARAM lparam)
  110. {
  111.  
  112.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  113.  
  114. }
  115.  
  116. LRESULT CALLBACK ChildWndProc(HWND   hwnd, 
  117.                          UINT   uMessage, 
  118.                          WPARAM wparam, 
  119.                          LPARAM lparam)
  120. {
  121.  
  122.     return DispMessage(&msdiChild, hwnd, uMessage, wparam, lparam);
  123.  
  124. }
  125. //
  126. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  127. //
  128. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  129. //
  130. //  PARAMETERS:
  131. //    hwnd     - window handle
  132. //    uMessage - WM_COMMAND (Unused)
  133. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  134. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  135. //
  136. //  RETURN VALUE:
  137. //    The return value depends on the message number.  If the message
  138. //    is implemented in the message dispatch table, the return value is
  139. //    the value returned by the message handling function.  Otherwise,
  140. //    the return value is the value returned by the default window procedure.
  141. //
  142. //  COMMENTS:
  143. //    Call the DispCommand() function with the main window's command dispatch
  144. //    information (cmdiMain) and the command specific information.
  145. //
  146.  
  147. #pragma argsused
  148. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  149. {
  150.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  151. }
  152.  
  153.  
  154. //
  155. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  156. //
  157. //  PURPOSE: Calls PostQuitMessage().
  158. //
  159. //  PARAMETERS:
  160. //
  161. //    hwnd      - Window handle  (Unused)
  162. //    uMessage  - Message number (Unused)
  163. //    wparam    - Extra data     (Unused)
  164. //    lparam    - Extra data     (Unused)
  165. //
  166. //  RETURN VALUE:
  167. //
  168. //    Always returns 0 - Message handled
  169. //
  170. //  COMMENTS:
  171. //
  172. //
  173.  
  174. #pragma argsused
  175. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  176. {
  177.      PostQuitMessage(0);
  178.      return 0;
  179. }
  180.  
  181.  
  182. //
  183. //  FUNCTION: MsgNotify(HWND, UINT, WPARAM, LPARAM)
  184. //
  185. //  PURPOSE:  Handles the Notification message sent from the TAB CONTROL
  186. //
  187. //  PARAMETERS:
  188. //
  189. //    hwnd      - Window handle  (Unused)
  190. //    uMessage  - Message number (Unused)
  191. //    wparam    - Extra data     (Unused)
  192. //    lparam    - Extra data     (Unused)
  193. //
  194. //  RETURN VALUE:
  195. //
  196. //    Always returns 0 - Message handled
  197. //
  198. //  COMMENTS:
  199. //
  200. //
  201.  
  202. #pragma argsused
  203. LRESULT MsgNotify(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  204. {
  205.      LPNMHDR    lpnmhdr = (LPNMHDR)lparam;
  206.     USHORT  ctrlRetVal, shftRetVal;
  207.     int        tabIndex;
  208.  
  209.     if (lpnmhdr->code == TCN_SELCHANGE)
  210.         InvalidateRect (hwndDisplayChild, NULL, TRUE);
  211.  
  212.     else if(lpnmhdr->code == TCN_KEYDOWN)
  213.     {
  214.         TC_KEYDOWN FAR * ptcKeydown = (TC_KEYDOWN FAR *)lparam;
  215.  
  216.         if(ptcKeydown->wVKey == VK_TAB)
  217.         {
  218.          // MessageBeep(MB_OK);
  219.             ctrlRetVal = GetKeyState(VK_CONTROL);
  220.             shftRetVal = GetKeyState(VK_SHIFT);
  221.             if((ctrlRetVal & 0x8000) && (shftRetVal & 0x8000))
  222.             {
  223.                
  224.               // The ctrl Key and the shift key are down, 
  225.               // Move the focus to the previous tab in the tab control. 
  226.               // If the focus is on the first tab, move focus to the
  227.               // last tab. 
  228.  
  229.                 tabIndex = (int) SendMessage(hwndTabControl, 
  230.                                              TCM_GETCURSEL,
  231.                                              (WPARAM)0,
  232.                                              (LPARAM)0);                
  233.                 if(tabIndex == -1)
  234.                 {
  235.                     MessageBox(NULL, "INVALID SELECTION - 1", "ERROR", MB_OK);
  236.                     return 0;
  237.                 }
  238.  
  239.                 else if(tabIndex == 0)
  240.                     SendMessage(hwndTabControl, 
  241.                                 TCM_SETCURSEL, 
  242.                                 (WPARAM)(int)NUMTABS - 1, 
  243.                                 (LPARAM)0);
  244.  
  245.                 else
  246.                     SendMessage(hwndTabControl, 
  247.                                 TCM_SETCURSEL,
  248.                                 (WPARAM)(int)tabIndex - 1, 
  249.                                 (LPARAM)0);
  250.  
  251.                 InvalidateRect (hwndDisplayChild, NULL, TRUE);
  252.             }
  253.  
  254.             else if(ctrlRetVal & 0x8000)
  255.             { 
  256.                          
  257.              // The Ctrl key is down..when tab was pressed
  258.              // Move the focus to the next tab in the tab control. 
  259.              // If the focus is on the last tab, move focus to the
  260.              // first tab.
  261.  
  262.              tabIndex = (int) SendMessage(hwndTabControl, 
  263.                                          TCM_GETCURSEL,
  264.                                           (WPARAM)0,
  265.                                          (LPARAM)0);
  266.  
  267.              if(tabIndex == -1)
  268.                 {
  269.                     MessageBox(NULL, "INVALID SELECTION - 1", "ERROR", MB_OK);
  270.                     return 0;
  271.                 }
  272.  
  273.                 else if(tabIndex == NUMTABS - 1)
  274.                     SendMessage(hwndTabControl, 
  275.                                 TCM_SETCURSEL, 
  276.                                 (WPARAM)(int)0,
  277.                                 (LPARAM)0);
  278.  
  279.                  else
  280.                     SendMessage(hwndTabControl, 
  281.                                 TCM_SETCURSEL, 
  282.                                 (WPARAM)(int)tabIndex + 1, 
  283.                                 (LPARAM)0);
  284.  
  285.  
  286.                 InvalidateRect (hwndDisplayChild, NULL, TRUE);
  287.  
  288.             }
  289.  
  290.  
  291.  
  292.         } // End of if(ptcKeydown->wVKey if statement
  293.  
  294.     }
  295.  
  296.     // Processes tooltip notification
  297.     else if (lpnmhdr->code == TTN_NEEDTEXT)
  298.     {
  299.  
  300.         LPTOOLTIPTEXT   lpToolTipText;
  301.         char szBuffer [80];
  302.  
  303.         lpToolTipText = (LPTOOLTIPTEXT)lparam;
  304.         LoadString(hInst,
  305.                    IDS_STARTDESC+lpToolTipText->hdr.idFrom,
  306.                    szBuffer,
  307.                    sizeof(szBuffer));
  308.         lpToolTipText->lpszText = szBuffer;
  309.  
  310.  
  311.  
  312.      }
  313.     return 0;
  314. }
  315.  
  316.  
  317. //
  318. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  319. //
  320. //  PURPOSE: Exit the application.
  321. //
  322. //  PARAMETERS:
  323. //    hwnd     - The window.
  324. //    wCommand - IDM_EXIT (unused)
  325. //    wNotify  - Notification number (unused)
  326. //    hwndCtrl - NULL (unused)
  327. //
  328. //  RETURN VALUE:
  329. //    Always returns 0 - command handled.
  330. //
  331. //  COMMENTS:
  332. //
  333. //
  334.  
  335. #pragma argsused
  336. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  337. {
  338.     DestroyWindow(hwnd);
  339.     return 0;
  340. }
  341.  
  342.  
  343. //
  344. //  FUNCTION: CmdDemo(HWND, WORD, WORD, HWND)
  345. //
  346. //  PURPOSE: Displays the "Demo" dialog box
  347. //
  348. //  PARAMETERS:
  349. //    hwnd      - Window handle
  350. //    wCommand  - IDM_DEMOTABCTRL     (unused)
  351. //    wNotify   - Notification number (unused)
  352. //    hwndCtrl  - NULL (unused)
  353. //
  354. //  RETURN VALUE:
  355. //
  356. //    Always returns 0 - Message handled
  357. //
  358. //  COMMENTS:
  359. //    To process the IDM_DEMOTABCTRL message, call CreateDialog which
  360. //    will create the box according to the information in your resource
  361. //    script file and turn control over to the Demo() function.
  362. //
  363. //
  364.  
  365. #pragma argsused
  366. LRESULT CmdDemo(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  367. {
  368.     HMENU hMenu;
  369.    
  370.     hMenu = GetMenu(hwnd);
  371.     if(DemoInit(hwnd))
  372.         EnableMenuItem(hMenu, 
  373.                        IDM_DEMOTABCTRL, 
  374.                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  375.     else
  376.         DestroyWindow(hwnd);
  377.  
  378.  
  379.  
  380.     return 0;
  381. }
  382.  
  383.  
  384. //
  385. //  FUNCTION: ChildMsgPaint(HWND, UINT, WPARAM, LPARAM)
  386. //
  387. //  PURPOSE: Processes WM_PAINT sent to display child window
  388. //
  389. //  PARAMETERS:
  390. //
  391. //    hwnd      - Window handle  (Unused)
  392. //    uMessage  - Message number (Unused)
  393. //    wparam    - Extra data     (Unused)
  394. //    lparam    - Extra data     (Unused)
  395. //
  396. //  RETURN VALUE:
  397. //
  398. //    Always returns 0 - Message handled
  399. //
  400. //  COMMENTS:
  401. //
  402. //
  403.  
  404. #pragma argsused
  405. LRESULT ChildMsgPaint(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  406. {
  407.     RECT rect;
  408.     HDC  hDC, hDC2;
  409.     BITMAP  bmp;
  410.     int   iCurSel;
  411.     HBITMAP  hBitmap, hOldBitmap;
  412.     PAINTSTRUCT ps;
  413.  
  414.  
  415.     hDC = BeginPaint (hwnd, &ps);
  416.  
  417.     // Get child window Rect
  418.     GetClientRect (hwndDisplayChild, &rect);
  419.  
  420.     // Which tab item is currently selected
  421.     iCurSel = TabCtrl_GetCurSel (hwndTabControl);
  422.  
  423.     // Load corresponding bitmap 
  424.     switch (iCurSel)
  425.     {
  426.         case 0: hBitmap = LoadBitmap (hInst, "SHAPES");
  427.                 break;
  428.         case 1: hBitmap = LoadBitmap (hInst, "CIRCLES");
  429.                 break;
  430.         case 2: hBitmap = LoadBitmap (hInst, "LEAVES");
  431.                 break;
  432.           case 3:
  433.         default:
  434.             hBitmap = LoadBitmap (hInst, "STREAMERS");
  435.             break;
  436.     }
  437.  
  438.     hDC2= CreateCompatibleDC (hDC);
  439.     GetObject(hBitmap, sizeof(BITMAP), &bmp);
  440.  
  441.     hOldBitmap = SelectObject(hDC2, hBitmap);
  442.     StretchBlt(hDC,  0, 0, rect.right, rect.bottom,
  443.                hDC2, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
  444.  
  445.      DeleteObject (SelectObject (hDC2, hOldBitmap));
  446.     DeleteDC (hDC2);
  447.  
  448.     EndPaint (hwnd, &ps);
  449.     return 0;
  450. }
  451.  
  452.